package com.stylefeng.guns.wx.config;

import com.stylefeng.guns.wx.handler.ContactHandler;
import com.stylefeng.guns.wx.handler.LogHandler;
import com.stylefeng.guns.wx.handler.MsgHandler;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Configuration
@ConditionalOnClass(WxCpService.class)
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfiguration {

    @Autowired
    private WxCpProperties properties;

    @Autowired
    private LogHandler logHandler;

    @Autowired
    private MsgHandler msgHandler;

    @Autowired
    private ContactHandler contactHandler;

    @Bean
    @ConditionalOnMissingBean
    public WxCpConfigStorage configStorage() {
        WxCpInMemoryConfigStorage configStorage = new WxCpInMemoryConfigStorage();
        configStorage.setCorpId(this.properties.getCorpId());
        configStorage.setAgentId(this.properties.getAgentId());
        configStorage.setCorpSecret(this.properties.getSecret());
        configStorage.setToken(this.properties.getToken());
        configStorage.setAesKey(this.properties.getAesKey());
        return configStorage;
    }

    @Bean
    @ConditionalOnMissingBean
    public WxCpService WxCpService(WxCpConfigStorage configStorage) {
        //WxCpService WxCpService = new me.chanjar.weixin.cp.api.impl.okhttp.WxCpServiceImpl();
        //WxCpService WxCpService = new me.chanjar.weixin.cp.api.impl.jodd.WxCpServiceImpl();
        //WxCpService WxCpService = new me.chanjar.weixin.cp.api.impl.apache.WxCpServiceImpl();
        WxCpService service = new me.chanjar.weixin.cp.api.impl.WxCpServiceImpl();
        service.setWxCpConfigStorage(configStorage);
        return service;
    }

    @Bean
    public WxCpMessageRouter router(WxCpService wxCpService) {
        final WxCpMessageRouter newRouter = new WxCpMessageRouter(wxCpService);

        //// 记录所有事件的日志 （异步执行）
        newRouter.rule().handler(this.logHandler).next();

        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event("change_contact").handler(this.getContactHandler()).end();

        // 默认
        newRouter.rule().async(false).handler(this.getMsgHandler()).end();
        return newRouter;
    }

    protected MsgHandler getMsgHandler() {
        return this.msgHandler;
    }

    protected ContactHandler getContactHandler() {
        return contactHandler;
    }
}
